/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainFrame.java
 *
 * Created on 2010-10-25, 15:03:39
 */
package predictor;

import dataanalysis.Model;
import java.awt.Component;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ItemEvent;
import java.io.File;
import java.util.Vector;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.JOptionPane;
import predictor.gui.dataviewer.DataTableView;
import weka.core.Instances;

/**
 *
 * @author Administrator
 */
public class MainFrame extends javax.swing.JFrame {

    private final MainView m_manager;
    private final Vector<String> m_strVector = new Vector();
    private final Vector<Model> m_modelVector = new Vector();
    private final Vector<Boolean> m_bFlags = new Vector();
    //private int m_selectedIndex = -1;
    private final ModelPanel m_modelPanel = new ModelPanel(this);
    private final DataTableView m_dataTable = new DataTableView(100, 100);

    /** Creates new form MainFrame */
    public MainFrame(String title, MainView manager) {
        super(title);
        m_manager = manager;
        initComponents();
        m_tabPanel.addTab("数据视图", m_dataTable);
        m_tabPanel.addTab("模型视图", m_modelPanel);
        Toolkit tk = Toolkit.getDefaultToolkit();
        Image image = tk.createImage("src\\predictor\\gui\\img\\icon.png");
        this.setIconImage(image);
        m_modelComb.setRenderer(new CombCellRender());
        this.setLocationRelativeTo(null);
        jToolBar1.setFloatable(false);
        jToolBar2.setFloatable(false);
        this.setVisible(false);
        this.addWindowListener(new java.awt.event.WindowAdapter() {

            @Override
            public void windowClosing(java.awt.event.WindowEvent e) {
                exit();
            }
        });
    }

    private void setDeleteButtonEnabled(boolean b) {
        m_deleteModelButton.setEnabled(b);
        m_deleteItem.setEnabled(b);
    }

    public void setStatusTxt(String string) {
        m_statusTxt.setText(string);
    }

    public void setCorrectPercent(String string) {
        m_percentTxt.setText(string);
    }

    private void exit() {
        if (!m_manager.isModified()) {
            System.exit(0);
            return;
        }
        int retVal = JOptionPane.showConfirmDialog(this, "是否保存预测结果？");
        if (JOptionPane.YES_OPTION == retVal) {
            if (!m_manager.saveResultData()) {//保存取消
                return;
            }
        } else if (JOptionPane.CANCEL_OPTION == retVal || JOptionPane.CLOSED_OPTION == retVal) {
            return;
        }
        System.exit(0);
    }

    class CombCellRender extends DefaultListCellRenderer {

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (index == -1) {
                return this;
            }
            if (m_bFlags.get(index).booleanValue() == false) {
                setEnabled(false);
                setOpaque(true);
            }
            return this;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jToolBar1 = new javax.swing.JToolBar();
        jButton1 = new javax.swing.JButton();
        m_saveButton = new javax.swing.JButton();
        m_addModelButton = new javax.swing.JButton();
        m_deleteModelButton = new javax.swing.JButton();
        m_startButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        m_statusTxt = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        m_numAttTxt = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        m_numInstTxt = new javax.swing.JLabel();
        jToolBar2 = new javax.swing.JToolBar();
        jPanel3 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        m_actrulClassComb = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        m_modelComb = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        m_percentTxt = new javax.swing.JLabel();
        m_tabPanel = new javax.swing.JTabbedPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        m_saveMenuItem = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jCheckBoxMenuItem1 = new javax.swing.JCheckBoxMenuItem();
        jRadioButtonMenuItem1 = new javax.swing.JRadioButtonMenuItem();
        jRadioButtonMenuItem2 = new javax.swing.JRadioButtonMenuItem();
        jMenu2 = new javax.swing.JMenu();
        m_addItem = new javax.swing.JMenuItem();
        m_deleteItem = new javax.swing.JMenuItem();
        m_startItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

        jToolBar1.setRollover(true);
        jToolBar1.setPreferredSize(new java.awt.Dimension(221, 30));

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/predictor/gui/img/open.png"))); // NOI18N
        jButton1.setToolTipText("打开数据");
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton1);

        m_saveButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/predictor/gui/img/save.png"))); // NOI18N
        m_saveButton.setToolTipText("保存数据");
        m_saveButton.setEnabled(false);
        m_saveButton.setFocusable(false);
        m_saveButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        m_saveButton.setPreferredSize(new java.awt.Dimension(39, 57));
        m_saveButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        m_saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_saveButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(m_saveButton);

        m_addModelButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/predictor/gui/img/add2.png"))); // NOI18N
        m_addModelButton.setToolTipText("添加模型");
        m_addModelButton.setFocusable(false);
        m_addModelButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        m_addModelButton.setPreferredSize(new java.awt.Dimension(32, 32));
        m_addModelButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        m_addModelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_addModelButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(m_addModelButton);

        m_deleteModelButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/predictor/gui/img/delete.png"))); // NOI18N
        m_deleteModelButton.setToolTipText("删除模型");
        m_deleteModelButton.setEnabled(false);
        m_deleteModelButton.setFocusable(false);
        m_deleteModelButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        m_deleteModelButton.setPreferredSize(new java.awt.Dimension(55, 57));
        m_deleteModelButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        m_deleteModelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_deleteModelButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(m_deleteModelButton);

        m_startButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/predictor/gui/img/start.png"))); // NOI18N
        m_startButton.setToolTipText("开始预测");
        m_startButton.setEnabled(false);
        m_startButton.setFocusable(false);
        m_startButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        m_startButton.setPreferredSize(new java.awt.Dimension(55, 57));
        m_startButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        m_startButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_startButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(m_startButton);

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel2.setText("当前状态：");

        m_statusTxt.setText("准备就绪");

        jLabel7.setText("变量:");

        m_numAttTxt.setText("0");

        jLabel9.setText("数据:");

        m_numInstTxt.setText("0");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(m_statusTxt)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 480, Short.MAX_VALUE)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(m_numAttTxt)
                .addGap(18, 18, 18)
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(m_numInstTxt)
                .addGap(39, 39, 39))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(m_statusTxt)
                    .addComponent(jLabel7)
                    .addComponent(m_numAttTxt)
                    .addComponent(jLabel9)
                    .addComponent(m_numInstTxt))
                .addContainerGap(12, Short.MAX_VALUE))
        );

        jToolBar2.setRollover(true);
        jToolBar2.setPreferredSize(new java.awt.Dimension(507, 30));

        jLabel4.setText("预测精度：");

        m_actrulClassComb.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                m_actrulClassCombItemStateChanged(evt);
            }
        });

        jLabel3.setText("实际类别：");

        m_modelComb.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                m_modelCombItemStateChanged(evt);
            }
        });

        jLabel1.setText("模型：");

        m_percentTxt.setText("无");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(16, 16, 16)
                .addComponent(m_modelComb, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(m_actrulClassComb, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(m_percentTxt)
                .addContainerGap(44, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(m_modelComb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel1)
                .addComponent(jLabel3)
                .addComponent(m_actrulClassComb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel4)
                .addComponent(m_percentTxt))
        );

        jToolBar2.add(jPanel3);

        m_tabPanel.setTabPlacement(javax.swing.JTabbedPane.BOTTOM);
        m_tabPanel.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                m_tabPanelStateChanged(evt);
            }
        });

        jMenu1.setText("文件");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setText("打开");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        m_saveMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        m_saveMenuItem.setText("保存");
        m_saveMenuItem.setEnabled(false);
        m_saveMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_saveMenuItemActionPerformed(evt);
            }
        });
        jMenu1.add(m_saveMenuItem);

        jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem3.setText("退出");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem3);

        jMenuBar1.add(jMenu1);

        jMenu3.setText("查看");

        jCheckBoxMenuItem1.setSelected(true);
        jCheckBoxMenuItem1.setText("工具栏");
        jCheckBoxMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBoxMenuItem1ActionPerformed(evt);
            }
        });
        jMenu3.add(jCheckBoxMenuItem1);

        buttonGroup1.add(jRadioButtonMenuItem1);
        jRadioButtonMenuItem1.setSelected(true);
        jRadioButtonMenuItem1.setText("数据视图");
        jRadioButtonMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonMenuItem1ActionPerformed(evt);
            }
        });
        jMenu3.add(jRadioButtonMenuItem1);

        buttonGroup1.add(jRadioButtonMenuItem2);
        jRadioButtonMenuItem2.setText("模型视图");
        jRadioButtonMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonMenuItem2ActionPerformed(evt);
            }
        });
        jMenu3.add(jRadioButtonMenuItem2);

        jMenuBar1.add(jMenu3);

        jMenu2.setText("模型");

        m_addItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
        m_addItem.setText("添加");
        m_addItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_addItemActionPerformed(evt);
            }
        });
        jMenu2.add(m_addItem);

        m_deleteItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_D, java.awt.event.InputEvent.CTRL_MASK));
        m_deleteItem.setText("删除");
        m_deleteItem.setEnabled(false);
        m_deleteItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_deleteItemActionPerformed(evt);
            }
        });
        jMenu2.add(m_deleteItem);

        m_startItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F5, java.awt.event.InputEvent.CTRL_MASK));
        m_startItem.setText("预测");
        m_startItem.setEnabled(false);
        m_startItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_startItemActionPerformed(evt);
            }
        });
        jMenu2.add(m_startItem);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 507, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(32, Short.MAX_VALUE))
            .addComponent(m_tabPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 751, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(m_tabPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 397, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void m_addModelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_addModelButtonActionPerformed
        this.addModelAction();
    }//GEN-LAST:event_m_addModelButtonActionPerformed

    private void m_saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_saveButtonActionPerformed
        m_manager.saveResultData();
    }//GEN-LAST:event_m_saveButtonActionPerformed

    private void m_startButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_startButtonActionPerformed
        m_manager.startPredict(m_modelVector.get(m_modelComb.getSelectedIndex()));
    }//GEN-LAST:event_m_startButtonActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        m_manager.OpenNewDataFile();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void m_deleteModelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_deleteModelButtonActionPerformed
        this.deleteSelectedModel();
    }//GEN-LAST:event_m_deleteModelButtonActionPerformed

    private void m_modelCombItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_m_modelCombItemStateChanged

        int i = m_modelComb.getSelectedIndex();
        if (evt.getStateChange() == ItemEvent.DESELECTED && i != -1) {//StateChange 2次，取消原来的，选择现在的
            return;
        }
        boolean b;
        b = i == -1 ? false : true;
        this.setDeleteButtonEnabled(b);

        m_percentTxt.setText("无");
        b = i == -1 ? false : m_bFlags.get(i).booleanValue();
        this.setStartButtonEnabeled(b);
        this.setModelText();
    }//GEN-LAST:event_m_modelCombItemStateChanged

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (m_manager.isModified()) {
            int retVal = JOptionPane.showConfirmDialog(this, "是否保存预测结果？");
            if (JOptionPane.YES_OPTION == retVal) {
                if (!m_manager.saveResultData()) {//保存取消
                    return;
                }
            } else if (JOptionPane.CANCEL_OPTION == retVal || JOptionPane.CLOSED_OPTION == retVal) {
                return;
            }
        }
        m_manager.OpenNewDataFile();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void m_actrulClassCombItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_m_actrulClassCombItemStateChanged
        int i = m_actrulClassComb.getSelectedIndex();
        if (i == -1) {
            return;
        }
        m_percentTxt.setText("无");
        m_manager.setDataClassIndex(i - 1);
        this.updateModelCapabilities();
    }//GEN-LAST:event_m_actrulClassCombItemStateChanged

    private void m_startItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_startItemActionPerformed
        m_manager.startPredict(m_modelVector.get(m_modelComb.getSelectedIndex()));
    }//GEN-LAST:event_m_startItemActionPerformed

    private void m_saveMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_saveMenuItemActionPerformed
        m_manager.saveResultData();
    }//GEN-LAST:event_m_saveMenuItemActionPerformed

    private void m_addItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_addItemActionPerformed
        this.addModelAction();
    }//GEN-LAST:event_m_addItemActionPerformed

    private void m_deleteItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_deleteItemActionPerformed
        this.deleteSelectedModel();
    }//GEN-LAST:event_m_deleteItemActionPerformed

    private void jCheckBoxMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxMenuItem1ActionPerformed
        boolean b = !jToolBar1.isVisible();
        jToolBar1.setVisible(b);
        jCheckBoxMenuItem1.setSelected(b);
    }//GEN-LAST:event_jCheckBoxMenuItem1ActionPerformed

    private void jRadioButtonMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonMenuItem1ActionPerformed
        if (m_tabPanel.getSelectedIndex() != 0) {
            m_tabPanel.setSelectedIndex(0);
        }
    }//GEN-LAST:event_jRadioButtonMenuItem1ActionPerformed

    private void jRadioButtonMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonMenuItem2ActionPerformed
        if (m_tabPanel.getSelectedIndex() != 1) {
            m_tabPanel.setSelectedIndex(1);
        }
    }//GEN-LAST:event_jRadioButtonMenuItem2ActionPerformed

    private void m_tabPanelStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_m_tabPanelStateChanged
        int index = m_tabPanel.getSelectedIndex();
        jRadioButtonMenuItem1.setSelected(index == 0);
        jRadioButtonMenuItem2.setSelected(index == 1);
    }//GEN-LAST:event_m_tabPanelStateChanged

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        this.exit();
    }//GEN-LAST:event_jMenuItem3ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem1;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem2;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JToolBar jToolBar2;
    private javax.swing.JComboBox m_actrulClassComb;
    private javax.swing.JMenuItem m_addItem;
    private javax.swing.JButton m_addModelButton;
    private javax.swing.JMenuItem m_deleteItem;
    private javax.swing.JButton m_deleteModelButton;
    private javax.swing.JComboBox m_modelComb;
    private javax.swing.JLabel m_numAttTxt;
    private javax.swing.JLabel m_numInstTxt;
    private javax.swing.JLabel m_percentTxt;
    private javax.swing.JButton m_saveButton;
    private javax.swing.JMenuItem m_saveMenuItem;
    private javax.swing.JButton m_startButton;
    private javax.swing.JMenuItem m_startItem;
    private javax.swing.JLabel m_statusTxt;
    private javax.swing.JTabbedPane m_tabPanel;
    // End of variables declaration//GEN-END:variables

    public void setOpenData(Instances data) {
        m_dataTable.setInstances(data);
        m_actrulClassComb.removeAllItems();
        m_actrulClassComb.addItem("");
        for (int i = 0; i < data.numAttributes(); i++) {
            m_actrulClassComb.addItem(data.attribute(i).name());
        }
        m_tabPanel.setSelectedIndex(0);
        m_numAttTxt.setText(String.valueOf(data.numAttributes()));
        m_numInstTxt.setText(String.valueOf(data.numInstances()));
        m_percentTxt.setText("无");
    }

    public void updateModelCapabilities() {
        int size = m_modelVector.size();
        if (size == 0) {
            return;
        }
        for (int i = 0; i < m_modelVector.size(); i++) {
            Boolean b = m_manager.checkModelCapabilities(m_modelVector.get(i));
            m_bFlags.set(i, b);
        }
        boolean b = m_bFlags.get(m_modelComb.getSelectedIndex());
        this.setStartButtonEnabeled(b);
    }

    public void addModel(Model model, String absolutePath, boolean bEnabled) {
        if (model == null) {
            return;
        }
        String str = model.getType() + " @ " + absolutePath;
        m_strVector.add(str);
        m_modelVector.add(model);
        m_bFlags.add(bEnabled);
        String name = new File(absolutePath).getName();
        name = name.substring(0, name.lastIndexOf('.'));
        m_modelComb.addItem(makeObj(name));
    }

    private Object makeObj(final String item) {
        return new Object() {

            @Override
            public String toString() {
                return item;
            }
        };
    }

    public void deleteSelectedModel() {
        int i = m_modelComb.getSelectedIndex();
        if (i != -1) {
            m_modelComb.removeItemAt(i);//remove can change m_selectedIndex
            m_strVector.remove(i);
            m_modelVector.remove(i);
            m_bFlags.remove(i);
        }
    }

    public void addModelAction() {
        m_manager.addModelAction();
    }

    public void setResultData(Instances resultData) {
        m_dataTable.setInstances(resultData);
        m_tabPanel.setSelectedIndex(0);
    }

    public void setSaveButtonEnabeled(boolean b) {
        m_saveButton.setEnabled(b);
        m_saveMenuItem.setEnabled(b);
    }

    private void setStartButtonEnabeled(boolean b) {
        m_startButton.setEnabled(b);
        m_startItem.setEnabled(b);
    }

    private void setModelText() {
        int i = m_modelComb.getSelectedIndex();
        if (i != -1) {
            Model model = m_modelVector.get(i);
            //m_modelPanel.setList(model.getHeaderVector());
            m_modelPanel.setTreeModel(model.getHeaderStructure());
            m_modelPanel.setText(model.toString());
        } else {
            //m_modelPanel.setList(null);
            m_modelPanel.setTreeModel(null);
            m_modelPanel.setText(null);
        }
    }

    public void cancleWorker() {
        m_manager.cancleWorker();
    }

    public void setErrorRow(int i,int j){
        m_dataTable.setErrorRow(i,j);
    }
}
